﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AlmaDatabase;
using Alma.Models;

namespace Alma.Controllers
{
    public class CriterionController : Controller
    {
        //
        // GET: /Criterion/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Criterion/Create

        public ActionResult Create()
        {
            return View(new Criterion());
        }

        [HttpPost]
        public ActionResult Create(Criterion newCriterion)
        {
            if (!ModelState.IsValid)
            {
                return View(newCriterion);
            }
            using (var context = new AlmaDBEntities())
            {
                context.Criteria.AddObject(newCriterion);
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        
        //
        // GET: /Criterion/Edit/5
 
        public ActionResult Edit(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var criteria = from criterion in context.Criteria
                           where criterion.ID == id
                           select criterion;
                return View(criteria.First());
            }
        }

        //
        // POST: /Criterion/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Criterion criterion)
        {
            if (!ModelState.IsValid)
            {
                return View(criterion);
            }
            using (var context = new AlmaDBEntities())
            {
                var crit = from u in context.Criteria
                            where u.ID == id
                            select u;
                var editCrit = crit.First();
                editCrit.Name = criterion.Name;
                editCrit.Unit = criterion.Unit;
                context.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        //
        // GET: /Criterion/Delete/5
 
        public ActionResult Delete(int id)
        {
            using (var context = new AlmaDBEntities())
            {
                var criteria = from criterion in context.Criteria
                              where criterion.ID == id
                              select criterion;
                return View((criteria.First()));
            };
        }

        //
        // POST: /Criterion/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Criterion delCriterion)
        {
            if (ModelState.IsValid)
            {
                using (var context = new AlmaDBEntities())
                {
                    var criteria = from criterions in context.Criteria
                                  where criterions.ID == id
                                  select criterions;
                    context.Criteria.DeleteObject(criteria.First());
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(delCriterion);
        }
    }
}
